package com.dingding.service.user.core.taxi.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.beans.special.Specialuservehicleinfo;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.taxi.ReqTaxiUserAuthDTO;
import com.dingding.facade.user.dto.taxi.ReqTaxiUserReAuthDTO;
import com.dingding.facade.user.dto.taxi.RespTaxiUserAuthDTO;
import com.dingding.facade.user.dto.taxi.RespTaxiUserReAuthDTO;
import com.dingding.facade.user.dto.taxi.enu.CarAuthStatus;
import com.dingding.service.user.core.taxi.dao.ITaxiUserDao;
import com.dingding.service.user.core.taxi.service.ITaxiUserService;

/**
 * 
 * @ClassName: TaxiDriverUserServiceImpl
 * @Description: 出租车司机相关业务实现
 * @author haoxiaojun
 * @date 2016年11月21日 下午4:25:13
 */
@Component("taxiUserService")
public class TaxiUserServiceImpl implements ITaxiUserService {

	@Resource(name = "taxiUserDao")
	private ITaxiUserDao taxiUserDao;

	/**
	 * @Title: TaxiUserController.java
	 * @Package com.dingding.special.controller
	 * @Description: 出租车主认证
	 * @author haoxiaojun
	 * @date 2016年11月21日 下午1:40:45
	 * @version V2.5.6
	 */
	@Override
	public FacadeResult<RespTaxiUserAuthDTO> taxiUserAuth(ReqTaxiUserAuthDTO taxiUser){
			
		RespTaxiUserAuthDTO respTaxiUserAuth = new RespTaxiUserAuthDTO();
		FacadeResult<RespTaxiUserAuthDTO> results = new FacadeResult<>();
		//把车辆认证信息存入数据库
		try {
			Specialuservehicleinfo specialuservehicleinfo = this.taxiUserDao.saveTaxiUserVehicleInfo(taxiUser);
			
			//修改用户登录表角色为相应的认证类型
			taxiUserDao.changeUserRole(specialuservehicleinfo.getUserId(), taxiUser.getUserRole());
			
			//respTaxiUserAuth.setJszImage(ToolUtil.imageAddress(specialuservehicleinfo.getJszImage()));
			respTaxiUserAuth.setXszImage(ToolUtil.imageAddress(specialuservehicleinfo.getXszImage()));
			respTaxiUserAuth.setVehicleImage(ToolUtil.imageAddress(specialuservehicleinfo.getVehicleImage()));
			respTaxiUserAuth.setSuvStatus(specialuservehicleinfo.getSuvStatus());
			
			results.setCode(200);
			results.setMessage("success");
			results.setObject(respTaxiUserAuth);
			
		} catch (BussinessException e) {
			results.setCode(e.getCode());
			results.setMessage(e.getErrorMessage());
		} catch (Exception e){
			results.setCode(500);
			results.setMessage("服务器异常");
		}
		
		return results;
		
	}

	/**
	 * @Title: TaxiUserController.java
	 * @Package com.dingding.special.controller
	 * @Description: 出租车主重新认证
	 * @author haoxiaojun
	 * @date 2016年11月21日 下午3:41:03
	 * @version V2.5.6
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public FacadeResult<RespTaxiUserReAuthDTO> reTaxiUserAuth(ReqTaxiUserReAuthDTO taxiUserReAuth) {
		//改变车辆审核的状态为重新认证状态
		boolean flag = this.taxiUserDao.changeCarAuthStatus(taxiUserReAuth.getUserId(), CarAuthStatus.RE_AUTH);
		
		FacadeResult<RespTaxiUserReAuthDTO> results = new FacadeResult<RespTaxiUserReAuthDTO>();
		if(flag){
			results.setCode(200);
			results.setMessage("success");
			results.setObject(new RespTaxiUserReAuthDTO(CarAuthStatus.RE_AUTH.getCode()));
		}else{
			results.setCode(400);
			results.setMessage("响应值为空");
			results.setObject(null);
		}
		return results;
	}

}
